import Vue from 'vue'
import Vuex from 'vuex'
import {
    getConnectionId, getToken, getUsername, removeConnectionId,
    removeToken, removeUsername, setConnectionId,
    setToken, setUsername
} from "@/utils/storage";
import {connect, disConnect} from "@/api/connection";

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        connectionId: getConnectionId() === undefined ? null : getConnectionId(),
        databaseName: null,
        collectionName: null,
        token: getToken() === undefined ? null : getToken(),
        username: getUsername() === undefined ? null : getUsername(),
    },
    mutations: {
        SET_CONNECTION_ID: (state, connectionId) => {
            state.connectionId = connectionId
        },
        SET_DATABASE_NAME: (state, databaseName) => {
            state.databaseName = databaseName
        },
        SET_COLLECTION_NAME: (state, collectionName) => {
            state.collectionName = collectionName
        },
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_USERNAME: (state, username) => {
            state.username = username
        },
    },
    actions: {
        // 连接
        Connect({commit}, connection) {
            return new Promise((resolve, reject) => {
                connect(connection).then(res => {
                    setConnectionId(res.data.connectionId)
                    commit('SET_CONNECTION_ID', res.data.connectionId)
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 断开连接
        DisConnect({commit}, connectionId) {
            return new Promise((resolve, reject) => {
                disConnect(connectionId).then(() => {
                    commit('SET_CONNECTION_ID', null)
                    commit('SET_DATABASE_NAME', null)
                    commit('SET_CONNECTION_ID', null)
                    removeConnectionId()
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },
        SetDatabaseName({commit}, databaseName) {
            commit('SET_DATABASE_NAME', databaseName)
        },
        SetCollectionName({commit}, collectionName) {
            commit('SET_COLLECTION_NAME', collectionName)
        },
        SetToken({commit}, token) {
            commit('SET_TOKEN', token)
            if (token === null) {
                removeToken()
            } else {
                setToken(token)
            }
        },
        SetUsername({commit}, username) {
            commit('SET_USERNAME', username)
            if (username === null) {
                removeUsername()
            } else {
                setUsername(username)
            }
        },
    },
    modules: {},
    getters: {
        getConnectionId(state) {
            return state.connectionId
        },
        getDatabaseName(state) {
            return state.databaseName
        },
        getCollectionName(state) {
            return state.collectionName
        },
        getToken(state) {
            return state.token
        },
        getUsername(state) {
            return state.username
        },
    }
})
